package com.cn.qgx428.lazydiary.ui.activity

import android.graphics.Color
import android.os.Bundle
import com.cn.qgx428.lazydiary.Presenter.UserPresenter
import com.cn.qgx428.lazydiary.Presenter.contract.UserContract
import com.cn.qgx428.lazydiary.R
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.toast

class RegisterActivity : BaseActivity() {
    override fun initUI() {
        setStatuBarColor(Color.BLACK)
        hindActionBar()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        register_submit.setOnClickListener({
            if (checkInfo()){
                UserPresenter.register(register_phone.text.toString(),
                        register_psw.text.toString(),
                        register_nickname.text.toString(),
                        object: UserContract.IUserRegister{
                            override fun onRegister(msg: String) {
                                toast(msg)
                                finish()
                            }

                            override fun onRegisterFaild(msg: String) {
                                toast(msg)
                            }
                        })
            }
        })
    }

    private fun checkInfo(): Boolean {
        if (register_psw.text.toString().length < 6){
            toast("密码不能小于6位")
            return false
        } else if (register_phone.text.toString().length!=11){
            toast("手机号长度不正确")
            return false
        } else if (register_nickname.text.toString().length == 0){
            toast("请填写昵称")
            return false
        }
        return true
    }
}
